﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 主题视图基类       ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                     ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone                ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Wedn.Net.Common;
using Wedn.Net.Extensions;
using Wedn.Net.UI.Model;

namespace Wedn.Net.UI.Page
{
    /// <summary>
    /// 主题视图基类
    /// </summary>
    public abstract class WebViewPage<TModel> : BaseViewPage<TModel>
    {
        #region Properties
        /// <summary>
        /// 业务服务对象
        /// </summary>
        protected IBLL.IBLLFactory Service { get { return BLLFactory.BLLFactory.Instance; } }

        /// <summary>
        /// 主题所在URL
        /// </summary>
        protected string ThemeUrl { get { return Setting.CurrentThemePath; } }

        /// <summary>
        /// 页面执行数据库查询次数
        /// </summary>
        protected int Queries { get { return Statistics.Instance.QueryCount; } }

        /// <summary>
        /// 页面执行数据库查询次数
        /// </summary>
        protected double ExcuteTime { get { return Statistics.Instance.ExcuteTime.TotalSeconds; } }
        #endregion

        #region 模版常用方法

        #region 获取网站设置信息 +string SiteInfo(string name)
        /// <summary>
        /// 获取网站设置信息
        /// </summary>
        protected string SiteInfo(string name) { return Setting.Get(name); }
        #endregion

        #region 获取主题设置信息 +string ThemeInfo(string name)
        /// <summary>
        /// 获取主题设置信息
        /// </summary>
        protected string ThemeInfo(string name) { return Setting.Get(Setting.GetThemeOptionKey(name)); }
        #endregion

        #region 获取主题包资源文件URL +string GetThemeUrl(string relpath)
        /// <summary>
        /// 获取主题包资源文件URL
        /// </summary>
        /// <param name="relpath">相对路径</param>
        /// <returns>文件URL</returns>
        protected string GetThemeUrl(string relpath)
        {
            return ThemeUrl + relpath.TrimStart('/');
        }
        #endregion

        #region 引入以HTML编码字符串形式呈现的分部视图 +MvcHtmlString Include(string viewName)
        /// <summary>
        /// 引入以HTML编码字符串的形式呈现指定的分部视图。
        /// </summary>
        /// <param name="viewName">要呈现的分部视图的名称</param>
        /// <returns>以HTML编码字符串形式呈现的分部视图</returns>
        protected MvcHtmlString Include(string viewName)
        {
            return Html.Partial(viewName);
        }
        #endregion

        #region 渲染头部(Header)部分系统标签 +MvcHtmlString RenderHeader()
        /// <summary>
        /// 渲染头部(Header)部分系统标签
        /// </summary>
        /// <returns></returns>
        public MvcHtmlString RenderHeader()
        {
            var head = string.Format(WednStruct.MetaFormat, "author", Setting.MetaAuthor);
            return new MvcHtmlString(head);
        }
        #endregion

        #region 渲染菜单导航 +MvcHtmlString RenderMenuNav(string ulId = "nav", string ulClass = "nav", string subUlClass = "sub-menu", string itemClass = "menu-item")
        /// <summary>
        /// 渲染菜单导航
        /// </summary>
        /// <param name="ulId">总UL ID</param>
        /// <param name="ulClass">总UL 类名</param>
        /// <param name="subUlClass">子UL 类名</param>
        /// <param name="itemClass">单项类名</param>
        /// <returns>菜单导航Html</returns>
        public MvcHtmlString RenderMenuNav(string ulId = "nav", string ulClass = "nav", string subUlClass = "sub-nav", string itemClass = "menu-item")
        {
            var menu = new MenuNav(Context.Request.RawUrl, ulId, ulClass, subUlClass, itemClass);
            return menu.Render();
        }
        #endregion

        #region 渲染分页代码 +MvcHtmlString RenderPager(int totalCount, int current = 1, int pageSize = 10, int showCount = 9)
        /// <summary>
        /// 渲染分页代码
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="current"></param>
        /// <param name="pageSize"></param>
        /// <param name="showCount"></param>
        /// <returns></returns>
        public MvcHtmlString RenderPager(int totalCount, int current = 1, int pageSize = 10, int showCount = 9)
        {
            var handler = Context.CurrentHandler as MvcHandler;
            if (handler != null)
            {
                var route = handler.RequestContext.RouteData;
                return Url.Pager(route.Values["action"].ToString(), route.Values["controller"].ToString(), totalCount, current, pageSize, showCount);
            }
            return new MvcHtmlString(string.Empty);
        }
        #endregion

        #region 渲染分页代码 +MvcHtmlString RenderPager(string action,string controller,int totalCount, int current = 1, int pageSize = 10, int showCount = 9)
        /// <summary>
        /// 渲染分页代码
        /// </summary>
        /// <param name="action"></param>
        /// <param name="controller"></param>
        /// <param name="totalCount"></param>
        /// <param name="current"></param>
        /// <param name="pageSize"></param>
        /// <param name="showCount"></param>
        /// <returns></returns>
        public MvcHtmlString RenderPager(string action, string controller, int totalCount, int current = 1, int pageSize = 10, int showCount = 9)
        {
            return Url.Pager(action, controller, totalCount, current, pageSize, showCount);
        }
        #endregion

        #region 渲染脚注(Footer)部分系统标签 +MvcHtmlString RenderFooter()
        /// <summary>
        /// 渲染脚注(Footer)部分系统标签
        /// </summary>
        /// <returns></returns>
        public MvcHtmlString RenderFooter()
        {
            return new MvcHtmlString(string.Format(WednStruct.MetaFormat, "date", DateTime.Now));
        }
        #endregion

        #endregion
    }
}
